package com.hwz.gulimall.product.service.impl;

import com.hwz.gulimall.product.entity.BrandEntity;
import com.hwz.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwz.common.utils.PageUtils;
import com.hwz.common.utils.Query;

import com.hwz.gulimall.product.dao.CategoryBrandRelationDao;
import com.hwz.gulimall.product.entity.CategoryBrandRelationEntity;
import com.hwz.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void brandCascadingUpdate(Long brandId, String brandName) {
        this.baseMapper.cascadingUpdate(brandId, brandName);
    }

    @Override
    public void updateCategory(Long catId, String catName) {
        CategoryBrandRelationEntity relation = new CategoryBrandRelationEntity();

        relation.setCatelogId(catId);
        relation.setCatelogName(catName);

        this.update(relation,new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {

        List<CategoryBrandRelationEntity> list = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        if (list == null){
            return null;
        }

        List<Long> branIds = list.stream().map(item -> item.getBrandId()).collect(Collectors.toList());
        List<BrandEntity> brandList = brandService.listByIds(branIds);

        return brandList;
    }

}